package com.ljxt.dt.service.purchasingmanagementservice.impl;

import com.ljxt.dt.dao.purchasingmanagementdao.PurchaseInfoDao;
import com.ljxt.dt.dao.purchasingmanagementdao.impl.PurchaseInfoDaoImpl;
import com.ljxt.dt.domain.*;
import com.ljxt.dt.service.purchasingmanagementservice.PurchaseDetailService;
import com.ljxt.dt.service.purchasingmanagementservice.PurchaseInfoService;
import com.ljxt.dt.vo.purchasingmanagement.CgddHandlerInfo;
import com.ljxt.dt.vo.purchasingmanagement.PurchaseInfoSelect;
import com.ljxt.dt.vo.purchasingmanagement.PurchaseMainVo;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author 徐浩钦
 * @version 1.0.0
 * @date 2020/5/11 15:24
 * @copyright 老九学堂T11
 */
public class PurchaseInfoServiceImpl implements PurchaseInfoService {



    private static PurchaseInfoDao purchaseInfoDao = new PurchaseInfoDaoImpl();
    private static PurchaseDetailService purchaseDetailService = new PurchaseDetailServiceImpl();

    @Override
    public  List<PurchaseMainVo> selectAll(int pageIndex, int pageSize, PurchaseInfoSelect select) {
//        最终返回的结果
        List<PurchaseMainVo> list = new ArrayList<>();

        // 循环格式化时间的显示
        for (PurchaseMainVo purchaseMainVo : purchaseInfoDao.selectAll(pageIndex,pageSize,PurchaseMainVo.class,select)) {
            // 设置字符串的格式化时间的成员变量
//            purchaseMainVo.strTimeFormat();

            // 设置所属的商品对象集合
            purchaseMainVo.setGoodsInfos(
                    purchaseDetailService.selectGoodsByPurchaseId(purchaseMainVo.getOrderNo()));

            // 如何商品信息存在则设置采购订单关联的供应商对象
            if(purchaseMainVo.getGoodsInfos() !=null && purchaseMainVo.getGoodsInfos().size() > 0){
                purchaseMainVo.setSupplier(purchaseDetailService.selectSupplierInfoByGoods(purchaseMainVo.getGoodsInfos().get(0).getId()));
            }

            // 设置明细表的集合信息
            purchaseMainVo.setPurchaseDetails(
                    purchaseInfoDao.selectPurchaseDetailByPurchaseInfo(purchaseMainVo.getOrderNo()));

            // 设置金额
            purchaseMainVo.setMoney(purchaseInfoDao.sumMoney(purchaseMainVo.getOrderNo()));

            // 设置结果至集合
            list.add(purchaseMainVo);
        }

        return list;
    }

    @Override
    public List<GoodsInfo> selectGoodsByPurchaseId(int id) {
        return purchaseInfoDao.selectGoodsByPurchaseId(id);
    }

    @Override
    public Double sumMoney(String rodId) {
        return purchaseInfoDao.sumMoney(rodId);
    }

    @Override
    public Integer selectCountByAll(PurchaseInfoSelect select) {
        return purchaseInfoDao.selectCountByAll(select);
    }

    @Override
    public List<PurchaseDetail> selectPurchaseDetailByPurchaseInfo(String ordId) {
        return purchaseInfoDao.selectPurchaseDetailByPurchaseInfo(ordId);
    }

    @Override
    public Map<String, String> selectAttrAndCategory(Map<String, String> map) {

        return purchaseInfoDao.selectAttrAndCategory(map);
    }

    @Override
    public List<CgddHandlerInfo> selectHandlerByCgddId(String cgddId) {
        return purchaseInfoDao.selectHandlerByCgddId(cgddId);
    }

    @Override
    public HandlerInfo selectObjByCgdd(String cgdd) {
        return purchaseInfoDao.selectObjByCgdd(cgdd);
    }

    @Override
    public int insertIntoHandlerInfo(HandlerInfo handlerInfo) {
        return purchaseInfoDao.insertIntoHandlerInfo(handlerInfo);
    }

    @Override
    public List<BillState> selectBillstateAll() {
        return purchaseInfoDao.selectBillstateAll();
    }

    @Override
    public List<SupplierInfo> selectSupplierInfoAll() {
        return purchaseInfoDao.selectSupplierInfoAll();
    }

    @Override
    public int deletePurchaseInfoAndPurchaseDetail(String... orderNo) {
        return purchaseInfoDao.deletePurchaseInfoAndPurchaseDetail(orderNo);
    }

    @Override
    public boolean isStateIdDelete(String orderNo) {
        return purchaseInfoDao.isStateIdDelete(orderNo);
    }
}
